import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JSlider;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


/**
 * The panels with required layouts.
 */
public class ToolsPanel extends JPanel  implements ChangeListener{
	
	protected JPanel panel1;

	protected JPanel panel2;
	
	protected JPanel shapesPanel;
	
	protected JPanel shapeColorPanel;
	
	protected JSlider strokeSlider;
	protected JLabel strokeLabel;

	protected JButton borderColorButton;
	//protected JButton fillColorButton;

	protected Box toolBox;
	
	protected JColorChooser colorPallete;
	
	
	public ToolsPanel() {
		
		ButtonGroup buttonsGroup = new ButtonGroup();
		
		setBorder(BorderFactory.createTitledBorder("Tool box"));
		
		shapesPanel = new JPanel();
		shapesPanel.setLayout(new GridLayout(0,2));

		panel1 = new JPanel();
		panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
		
		panel2 = new JPanel();
		panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
		
		ImageIcon icon = createImageIcon("icons/rectangle.png");
		JToggleButton jtbRectangle = new JToggleButton(icon);
		panel1.add(jtbRectangle);
		
		icon = createImageIcon("icons/elipse.png");
		JToggleButton jtbEllipse = new JToggleButton(icon);
		panel1.add(jtbEllipse);
		
		icon = createImageIcon("icons/line.png");
		JToggleButton jtbLine = new JToggleButton(icon);
		panel1.add(jtbLine);
		
		icon = createImageIcon("icons/pentagon.png");
		JToggleButton jtbRRectangle = new JToggleButton(icon);
		panel2.add(jtbRRectangle);
		
		icon = createImageIcon("icons/triangle.png");
		JToggleButton jtbTriangle = new JToggleButton(icon);
		panel2.add(jtbTriangle);
		
		buttonsGroup.add(jtbRectangle);
		buttonsGroup.add(jtbEllipse);
		buttonsGroup.add(jtbLine);
		buttonsGroup.add(jtbRRectangle);
		buttonsGroup.add(jtbTriangle);
		//buttonsGroup.add(jtbCurve);
		
		strokeLabel = new JLabel("Stroke: ");
		strokeLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
		
		
		strokeSlider = new JSlider(JSlider.HORIZONTAL, 1, 10, 1);
		strokeSlider.setMajorTickSpacing(10);
		strokeSlider.setMinorTickSpacing(1);
		strokeSlider.setPaintTicks(true);
		strokeSlider.setPreferredSize(new Dimension(80, 52));
		strokeSlider.addChangeListener(this);
		
		strokeLabel.setText("Stroke: " + strokeSlider.getValue());
		
		shapesPanel.add(panel1);
		shapesPanel.add(panel2);
		
		shapeColorPanel = new JPanel();
		shapeColorPanel.setLayout(new GridLayout(2,0));
		shapeColorPanel.setBorder(BorderFactory.createTitledBorder("Colors"));

		borderColorButton = new JButton();
		borderColorButton.setBackground(Color.BLACK);
		borderColorButton.setOpaque(true);
		borderColorButton.setToolTipText("Border color");

		shapeColorPanel.add(borderColorButton);

		toolBox = new Box(BoxLayout.Y_AXIS);
		
		toolBox.add(shapesPanel);
		toolBox.add(new JSeparator(SwingConstants.HORIZONTAL));
		toolBox.add(strokeLabel);
		toolBox.add(strokeSlider);
		toolBox.add(new JSeparator(SwingConstants.HORIZONTAL));
		toolBox.add(shapeColorPanel);
		
		add(toolBox);
	}
			
	/** Returns an ImageIcon, or null if the path was invalid. */
	protected ImageIcon createImageIcon(String path) {
	    java.net.URL imgURL = this.getClass().getResource(path);
	    if (imgURL != null) {
	        return new ImageIcon(imgURL);
	    } else {
	        System.err.println("Couldn't find file: " + path);
	        return null;
	    }
	}

	public static final long serialVersionUID = 1;



	@Override
	public void stateChanged(ChangeEvent e) {
		strokeLabel.setText("Stroke: " + strokeSlider.getValue());
	}
}